package search

import (
	"fmt"
	"sync"
	"titargo/matcher"
	"titargo/resource"
)

/**
执行
*/
func Done(url string, seed int) {
	fmt.Println("starting spy")
	matcher.Baseurl = url
	imgresource := resource.NewImgResource()
	urlresource := resource.NewUrlResource()
	imgMap = imgresource.Read()
	urlMap = urlresource.Read()
	imgChan := make(chan *resource.Img, 1000)
	urlChan := make(chan *resource.Link, 1000)
	var waitGroup sync.WaitGroup
	waitGroup.Add(1)
	go func() {
		link := &resource.Link{Href: url, Content: ""}
		spy(link, seed, imgChan, urlChan)
		waitGroup.Done()
	}()
	go func() {
		getImgChan(imgChan)
	}()
	go func() {
		getUrlChan(urlChan)
	}()
	waitGroup.Wait()

	close(imgChan)
	close(urlChan)
	downloadAll()
	imgresource.Write(imgMap)
	urlresource.Write(urlMap)
	fmt.Println("done end")
}
